## R Script Output -------------------------------------------------------------
# Figure 1: Chinese FREI Exposure in the Contiguous United States by County


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("tidyverse",  
         "RColorBrewer", 
         "gridExtra", 
         "viridis",
         "choroplethr", 
         "choroplethrMaps")

lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/ISQ-frei-replication/"


## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "foia-county-cn-under-2012.RData", sep = ""))

data(continental_us_states)


## Figure 1 --------------------------------------------------------------------
# rename vars for package input
exposure.df <- foia.county.cn.under %>%
  rename(region = county_fips, 
         value = china_undergraduate_county_sqmi_2012) 

# plot and save
pdf(file = paste(MAIN_DIR, "Figure-1.pdf", sep = ""),
    width = 12, height = 8)
county_choropleth(exposure.df,
                  title = "",
                  state_zoom = continental_us_states) +
  scale_fill_manual(values = viridis(8,
                                     alpha = 1, begin = 0, end = 1,
                                     option = "A")[2:8],
                    labels = c("[0, 0.0001]",
                               "(0.0001, 0.0005]",
                               "(0.0005, 0.0019]",
                               "(0.0019, 0.0046]",
                               "(0.0046, 0.0137]",
                               "(0.0137, 0.0542]",
                               "(0.0542, 61]"),
                    name = "Exposure Level",
                    na.value = "grey90") +
  guides(color = guide_legend("my title"))
dev.off()


setEPS()
cairo_ps(paste(MAIN_DIR, "Figure-1.eps", sep = "/"), 
         width = 12, height = 8)
county_choropleth(exposure.df,
                  title = "",
                  state_zoom = continental_us_states) +
  scale_fill_manual(values = viridis(8,
                                     alpha = 1, begin = 0, end = 1,
                                     option = "A")[2:8],
                    labels = c("[0, 0.0001]",
                               "(0.0001, 0.0005]",
                               "(0.0005, 0.0019]",
                               "(0.0019, 0.0046]",
                               "(0.0046, 0.0137]",
                               "(0.0137, 0.0542]",
                               "(0.0542, 61]"),
                    name = "Exposure Level",
                    na.value = "grey90") +
  guides(color = guide_legend("my title"))
dev.off()
  